﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Bson;
using JXT.PrimaryKey.Batman.Domain.Quotation.Models;
using JXT.PrimaryKey.Batman.Domain.Quotation.Repositories;
using JXT.PrimaryKey.Batman.Domain.Repositories.Mongo;

namespace JXT.PrimaryKey.Batman.Domain.Repositories.Quotation
{
    public class MongoTimeSharingQuoteRepository : MongoRepositoryBase<TimeSharingQuote, Guid>, ITimeSharingQuoteRepository
    {
        public MongoTimeSharingQuoteRepository(RepositoryContext context)
            : base((MongoRepositoryContext)context)
        {
        }

        public override void Add(TimeSharingQuote entity)
        {
            if (entity == null)
                throw new ArgumentNullException("entity");
            entity.Id = Guid.NewGuid();
            //base.Add(entity);
            if (!IsCollectionExists)
                setIndex();
            base.Add(entity);
        }

        public void Delete(DateTime endTime)
        {
            IMongoQuery query = Query.LT("UpdateTime", endTime);
            MongoQuery.Remove(query, _defaultSafeMode);
        }

        public TimeSharingQuote GetSingle(string code, MarketType type = MarketType.Unknown)
        {
            IMongoQuery query = Query.EQ("Code", code);
            if (type != MarketType.Unknown)
                query = Query.And(query, Query.EQ("MarketType", type));
            return MongoQuery.FindAs<TimeSharingQuote>(query)
                .SetSortOrder(SortBy.Descending("UpdateTime"))
                .SetLimit(1)
                .FirstOrDefault();
        }

        public IEnumerable<TimeSharingQuote> GetList(string code, MarketType type = MarketType.Unknown, DateTime? startTime = null, DateTime? endTime = null)
        {
            IMongoQuery query = Query.EQ("Code", code);
            if (type != MarketType.Unknown)
                query = Query.And(query, Query.EQ("MarketType", type));
            if (startTime == null)
                query = Query.And(query, Query.GTE("UpdateTime", new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0)));
            else
                query = Query.And(query, Query.GTE("UpdateTime", startTime.Value));
            if (endTime != null)
                query = Query.And(query, Query.LT("UpdateTime", endTime.Value));
            return MongoQuery.FindAs<TimeSharingQuote>(query)
                .SetSortOrder(SortBy.Descending("UpdateTime"))
                .ToList();
        }

        private void setIndex()
        {
            IMongoIndexKeys keys = IndexKeys.Descending("UpdateTime");
            IMongoIndexOptions options = IndexOptions.SetUnique(false);
            MongoQuery.CreateIndex(keys, options);
            keys = IndexKeys.Ascending("Code", "MarketType");
            MongoQuery.CreateIndex(keys, options);
        }
    }
}
